// pages/tools/jiatingyaohao/index/index.js
var num = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        resultData: 0,
        inputValue: 0,
        scoreData: 0,
        typeData: [{
            peopleType: "主申请人",
            showladder: false,
            score: 0,
            name: "mainPeople",
            inputname: "mainPeopleInput",
            checkbox: [{
                value: 2,
                selectName: "参与家庭积分"
            }, {
                value: "参与摇号",
                selectName: "参与摇号",
            }]
        }, {
            peopleType: "配偶",
            showladder: false,
            score: 0,
            name: "peiou",
            inputname: "peiouInput",
            checkbox: [{
                value: 1,
                selectName: "参与家庭积分"
            }, {
                value: "参与摇号",
                selectName: "参与摇号",
            }]
        }, {
            peopleType: "主申请人的父亲",
            showladder: false,
            score: 0,
            name: "mainfather",
            inputname: "mainfatherInput",
            checkbox: [{
                value: 1,
                selectName: "参与家庭积分"
            }, {
                value: "参与摇号",
                selectName: "参与摇号",
            }]
        }, {
            peopleType: "主申请人的母亲",
            showladder: false,
            score: 0,
            name: "mainmother",
            inputname: "mainmotherInput",
            checkbox: [{
                value: 1,
                selectName: "参与家庭积分"
            }, {
                value: "参与摇号",
                selectName: "参与摇号",
            }]
        }, {
            peopleType: "配偶的父亲",
            showladder: false,
            name: "peioufather",
            inputname: "peioufatherInput",
            score: 0,
            checkbox: [{
                value: 1,
                selectName: "参与家庭积分"
            }, {
                value: "参与摇号",
                selectName: "参与摇号",
            }]
        }, {
            peopleType: "配偶的母亲",
            showladder: false,
            name: "peioumother",
            inputname: "peioumotherInput",
            score: 0,
            checkbox: [{
                value: 1,
                selectName: "参与家庭积分"
            }, {
                value: "参与摇号",
                selectName: "参与摇号",
            }]
        }, {
            peopleType: "子女",
            showladder: false,
            score: 0,
            name: "children",
            inputname: "childrenInput1",
            checkbox: [{
                value: 1,
                selectName: "参与家庭积分"
            }, {
                value: "参与摇号",
                selectName: "参与摇号",
            }]
        }],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    formSubmit(e) {
        let resData = e.detail.value;
        let first = (resData.children != '') || (resData.mainPeople != '' || resData.peiou != '') || (resData.mainfather != '' || resData.mainmother != '' || resData.peioufather != '' || resData.peioumother != '');
        let second = ((resData.mainPeople != '' || resData.peiou != '') && (resData.mainfather != '' || resData.mainmother != '' || resData.peioufather != '' || resData.peioumother != '')) || (resData.children != '' && (resData.mainPeople != '' || resData.peiou != '')) || resData.children != '' && (resData.mainfather != '' || resData.mainmother != '' || resData.peioufather != '' || resData.peioumother != '');
        let third = resData.children != '' && (resData.mainPeople != '' || resData.peiou != '') && (resData.mainfather != '' || resData.mainmother != '' || resData.peioufather != '' || resData.peioumother != '');
        let sum = 0;
        this.data.typeData.forEach((v, k) => {
            sum += Number(v.score)
        })
        if (third) {
            wx.showModal({
                title: '计算结果',
                content: '家庭积分' + (sum * 3),
                showCancel: false,
            })
        } else if (second) {
            wx.showModal({
                title: '计算结果',
                content: '家庭积分' + (sum * 2),
                showCancel: false,
            })
        } else if (first) {
            wx.showModal({
                title: '计算结果',
                content: '家庭积分' + (sum * 1),
                showCancel: false,
            })
        }
    },

    checkboxChange(e) {
        let index = e.currentTarget.dataset.index;
        let value = e.detail.value;
        let flag = value.find(item => item === "参与摇号");
        let scoreData = value.find(item => item != "参与摇号");
        if (flag == "参与摇号") {
            this.data.typeData.forEach((v, k) => {
                if (k == index) {
                    v.showladder = true;
                }
            })
            this.setData({
                typeData: this.data.typeData
            })
        } else {
            this.data.typeData.forEach((v, k) => {
                if (k == index) {
                    v.showladder = false;
                }
            })
            this.setData({
                typeData: this.data.typeData
            })
        }
        this.data.typeData.forEach((v, k) => {
            if (k == index) {
                v.score = Number(scoreData ? scoreData : 0) + Number(this.data.inputValue);
            }
        })
        this.setData({
            typeData: this.data.typeData,
            scoreData: scoreData ? scoreData : 0,
        })
    },

    addChildren(e) {
        num++;
        this.data.typeData.push({
            peopleType: "子女",
            showladder: false,
            score: 0,
            name: "children",
            inputname: "childrenInput" + num,
            checkbox: [{
                value: 1,
                selectName: "参与家庭积分"
            }, {
                value: "参与摇号",
                selectName: "参与摇号",
            }]
        })
        this.setData({
            typeData: this.data.typeData
        })
    },

    bindinput(e) {
        let index = e.currentTarget.dataset.index;
        let inputValue = e.detail.value;
        this.data.typeData.forEach((v, k) => {
            if (k == index) {
                v.score = Number(this.data.scoreData) + Number(inputValue);
            }
        })
        this.setData({
            typeData: this.data.typeData
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        return {
            title: "北京小客车家庭摇号积分计算器",
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
        }
    },

    // 分享朋友圈
    onShareTimeline: function() {
        return {
            title: "北京小客车家庭摇号积分计算器",
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
        }
    },
})